import forge from 'node-forge';
import { stringify } from 'qs';
import request from '../utils/request';

/** ***************************** other ****************************** */
export async function changeUserPassword(params, url) {
  return request('/api/user/system/user/changePassword', {
    method: 'POST',
    body: sign(params),
  });
}

/** ***************************** login ****************************** */
export async function accountLogin(params) {
  return request(`/api/user/system/realm/login?sign=${sign(params)}`, {
    method: 'POST',
    body: params,
  });
}

export async function logout() {
  return request(`/api/user/system/realm/logout?sign=${DEFAULT_SIGN}`);
}


export async function queryCurrent() {
  return request(`/api/user/system/realm/currentUser?sign=${DEFAULT_SIGN}`);
}


/** ***************************** entity ****************************** */
export async function queryEntityById(params, service, url) {
  return request(`/api${service}${url}/get/${params.id}?sign=${DEFAULT_SIGN}`);
}

export async function queryEntity(params, service, url) {
  return request(`/api${service}${url}/list?${stringify(params)}&sign=${sign(params)}`);
}

export async function queryEntityCustomer(params, service, url) {
  return request(`/api${service}${url}?${stringify(params)}&sign=${sign(params)}`);
}

export async function deleteEntity(params, service, url) {
  return request(`/api${service}${url}/delete?sign=${sign(params)}`, {
    method: 'POST',
    body: params,
  });
}

export async function createEntity(params, service, url) {
  return request(`/api${service}${url}/save?sign=${sign(params)}`, {
    method: 'POST',
    body: params,
  });
}

export async function updateEntity(params, service, url) {
  return request(`/api${service}${url}/update?sign=${sign(params)}`, {
    method: 'POST',
    body: params,
  });
}

export async function updateMoreEntity(params, service, url) {
  return request(`/api${service}${url}/more/update?sign=${sign(params)}`, {
    method: 'POST',
    body: params,
  });
}

export async function operateEntity(params, service, url, operate) {
  return request(`/api${service}${url}${operate}?sign=${sign(params)}`, {
    method: 'POST',
    body: params,
  });
}


export async function recommendProduct(params) { // 商品首页推荐 (后台管理
  return request(`/api/product/product/recommendProduct?sign=${sign(params)}`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: params,
  });
}



const DEFAULT_SIGN = '7FC7E243BB29A07128C699F68F3BED96';

export function sign(data) {
  let sign = '';
  if (data) {
    let message = '';
    for (const key of Object.keys(data).sort()) {

      if (Array.isArray(data[key]) || typeof data[key] === 'object') {
        // message = message + key + "[\"" + data[key].join("\",\"") + "\"]";
      } else if (data[key] != null && data[key] != undefined && data[key] !== '') {
        message = message + key + data[key];
      }
    }


    // console.log(data);
    // console.log(message);

    if (message === '') {
      return DEFAULT_SIGN;
    }

    const md = forge.md.md5.create();
    md.update(message, 'utf8');
    const mes1 = `${md.digest().toHex() }upmall`;

    const md2 = forge.md.md5.create();
    md2.update(mes1, 'utf8');
    sign = md2.digest().toHex().toUpperCase();

  } else {
    const md = forge.md.md5.create();
    md.update('upmall', 'utf8');
    sign = md.digest().toHex().toUpperCase();
  }

  return sign;
}
